package com.project.weimi.activity;

import java.util.HashMap;
import java.util.Map;

import com.project.weimi.R;
import com.project.weimi.application.Constant;
import com.project.weimi.entity.response.ResponseBean;
import com.project.weimi.interfaces.MyCallBack;
import com.project.weimi.utils.CheckUtils;
import com.project.weimi.utils.GsonUtil;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.SetClickListener;
import com.project.weimi.utils.SharedPreferencesHelper;
import com.project.weimi.utils.XUtil;
import com.project.weimi.widget.EditTextWithDel;
import com.project.weimi.widget.ToastMessage;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * 修改密码Activity
 * @author Administrator
 *
 */
public class SecretSetActivity extends BaseActivity{

	private Context mContext;
	private LinearLayout ll_setSecret,ll_changeSuccess;
	private EditTextWithDel et_newPwd,et_newPwdAgain;
	private Button btn_confirm;
	
	private String userPhone;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_secret_set, "设置密码");
		mContext = this;
		
		initView();
		
	}
	
	private void initView(){
		ll_setSecret = (LinearLayout)findViewById(R.id.ll_setSecret);
		ll_changeSuccess = (LinearLayout)findViewById(R.id.ll_changeSuccess);
		et_newPwd = (EditTextWithDel)findViewById(R.id.et_newPwd);
		et_newPwdAgain = (EditTextWithDel)findViewById(R.id.et_newPwdAgain);
		btn_confirm = (Button)findViewById(R.id.btn_confirm);
		
		// 监听
		SetClickListener.setClickListener(listener, btn_confirm);
		
	}
	
	private OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			if(!NetUtils.isConnected(SecretSetActivity.this)){
				ToastMessage.showToast(SecretSetActivity.this, "网络不给力，请检查网络设置");
			} else {
				initHttp();
			}
			
		}
	};
	
	private void initHttp(){
		String newPwd = et_newPwd.getText().toString().replace(" ", "").trim();
		String newPwdAgain = et_newPwdAgain.getText().toString().replace(" ", "").trim();
		
		if(TextUtils.isEmpty(newPwd)){
			ToastMessage.showMyToast(mContext, "请输入新密码！");
			return;
		}
		if(newPwd.length() < 6){
			ToastMessage.showMyToast(mContext, "密码长度在6-18位之间");
			return;
		}
		if(!newPwd.matches(CheckUtils.pwdPattern)){
			ToastMessage.showMyToast(mContext, "必须字母开头，可以为纯字母，也可以为数字、字母组合");
			return;
		}
		if(TextUtils.isEmpty(newPwdAgain)){
			ToastMessage.showMyToast(mContext, "请输入确认密码！");
			return;
		}
		if(!newPwdAgain.equals(newPwd)){
			ToastMessage.showMyToast(mContext, "再次输入的密码不一致！");
			return;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		map.put("passWord", newPwd);
		
		XUtil.post(Constant.CHANGE_PWD, map, new MyCallBack<String>(){
			
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(null != result && !"".equals(result)){
					//Log.e("TAG", "SecretSetActivity==="+result);
					ResponseBean bean = (ResponseBean) GsonUtil.jsonToBean(
							result, ResponseBean.class);
					if(null != bean){
						String str = bean.getResult();
						if(!TextUtils.isEmpty(str) && "1".equals(str)){
//							SharedPreferencesHelper spHelper = new SharedPreferencesHelper(
//									SecretSetActivity.this,Constant.USER_INFO);
							ll_setSecret.setVisibility(View.GONE);
							ll_changeSuccess.setVisibility(View.VISIBLE);
							
						} else {
							
						}
					}
				}
			}
			
			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(null != spHelper && !TextUtils.isEmpty(memberId)){
			//Log.e("TAG", "onStart----SecretSetActivity前memberId===---"+memberId);
			userPhone = memberId;
			//Log.e("TAG", "onStart----SecretSetActivity后userPhone===---"+userPhone);
		}
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		super.setStatusBar(R.color.transparent_3);
	}
	
}
